package ro.flashbite.game_engine.view
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.events.TouchEvent;
	import flash.ui.Multitouch;
	
	import ro.flashbite.game_engine.interfaces.IMenuToEngine;
	import ro.flashbite.game_engine.view.sheet.BaseSpriteSheet;
	import ro.flashbite.game_engine.view.sheet.FrameLabeledSpriteSheet;
	import ro.flashbite.game_engine.view.sheet.cpu.ComplexSpriteSheetCPU;
	import ro.flashbite.game_engine.view.sheet.cpu.SimpleSpriteSheetCPU;
	import ro.flashbite.helpers.MovieClipHelpers;
	import ro.flashbite.interfaces.ISleepWakeupDestroy;
	import ro.flashbite.loaders.types.consts.AssetTypes;
	import ro.flashbite.loggers.ILogger;
	import ro.flashbite.loggers.LoggerFactory;
	
	public class BaseMenu extends Sprite implements ISleepWakeupDestroy
	{
		/**
		 * this will be the parent on wich this will be added 
		 */		
		protected var _appContainer		:	Sprite;
		/**
		 * id of the menu specified in MENU_LIST of application 
		 */
		protected var _id				:	String;
		/**
		 * the logic xml of the Menu
		 */		
		protected var _logicXML			:	XML;
		/**
		 * reference to game engine 
		 */		
		protected var _gameEngine		:	IMenuToEngine;
		/**
		 * holds all interactive button on screen 
		 */		
		protected var _allInteractives	:	Vector.<BaseSpriteSheet>;
		/**
		 * background of the menu 
		 */		
		protected var _background		:	DisplayObject;
		/**
		 * logger for debugging
		 */
		protected var _logger			:	ILogger;
		/**
		 * type defined by MenuTypes class 
		 */		
		protected var _type				:	uint;
		/**
		 * holds back menu id; when pressing a back button, on this menu the application will go 
		 */		
		private var _backMenuID			:	String;
		/**
		 * holds last data given by this
		 */		
		protected var _data				:	*;
		
		// ================================================================================================================================
		// CONSTRUCTOR
		// ================================================================================================================================
		
		public function BaseMenu(appContainer:Sprite, id:String, gameEngine:IMenuToEngine, type:uint)
		{
			_constructor(appContainer, id, gameEngine, type);
		}
		private function _constructor(appContainer:Sprite, id:String, gameEngine:IMenuToEngine, type:uint):void
		{
			_appContainer = appContainer;
			_id = id;
			_gameEngine = gameEngine;
			_type = type;
			
			sleep();
			//add it to display list as invisible
			_appContainer.addChild(this); this.visible = false;
		}
		
		// ================================================================================================================================
		// ISleepWakeupDestroy
		// ================================================================================================================================
		
		public function sleep():void
		{
			this.mouseEnabled = this.mouseChildren = false;
		}
		
		public function wakeup():void
		{
			this.mouseEnabled = this.mouseChildren = true;
		}
		
		public function destroy():void
		{
			//sleep();
			
			if (_logger) {
				LoggerFactory.clearLogger(_logger.name);
				_logger = null;
			}
			
			_data = null;
			
			_appContainer = null;
			_id = null;
			_logicXML = null;
			_gameEngine = null;
			_allInteractives = null;
			_background = null;
			_backMenuID = null;
			
			MovieClipHelpers.removeAllChildren(this);
		}
		
		// ================================================================================================================================
		// PUBLIC
		// ================================================================================================================================
		
		public function show(data:* = null, useBack:Boolean = false):void
		{
			reset();
			
			this.visible = true;
			
			//_appContainer.addChild(this);
			
			wakeup();
		}
		
		public function hide():void
		{
			this.visible = false;
			
			//if (this.parent) this.parent.removeChild(this);
			
			sleep();
		}
		
		// ================================================================================================================================
		// PROTECTED
		// ================================================================================================================================
		
		protected function setInteraction(btn:BaseSpriteSheet, enable:Boolean):void
		{
			var isMobileDevice:Boolean = Multitouch.supportsTouchEvents;
			
			if (enable) {
				btn.enable();
				if (isMobileDevice) {
					btn.addEventListener(TouchEvent.TOUCH_BEGIN, onBtnTouchEvent, false, 0, true);
					btn.addEventListener(TouchEvent.TOUCH_ROLL_OVER, onBtnTouchEvent, false, 0, true);
					btn.addEventListener(TouchEvent.TOUCH_END, onBtnTouchEvent, false, 0, true);
					btn.addEventListener(TouchEvent.TOUCH_ROLL_OUT, onBtnTouchEvent, false, 0, true);
					btn.addEventListener(TouchEvent.TOUCH_TAP, onBtnTouchEvent, false, 0, true);
				} else {
					btn.buttonMode = true;
					btn.addEventListener(MouseEvent.MOUSE_DOWN, onBtnMouseEvent, false, 0, true);
					btn.addEventListener(MouseEvent.ROLL_OVER, onBtnMouseEvent, false, 0, true);
					btn.addEventListener(MouseEvent.MOUSE_UP, onBtnMouseEvent, false, 0, true);
					btn.addEventListener(MouseEvent.ROLL_OUT, onBtnMouseEvent, false, 0, true);
					btn.addEventListener(MouseEvent.CLICK, onBtnMouseEvent, false, 0, true);
				}
			} else {
				btn.disable();
				if (isMobileDevice) {
					btn.removeEventListener(TouchEvent.TOUCH_BEGIN, onBtnTouchEvent);
					btn.removeEventListener(TouchEvent.TOUCH_ROLL_OVER, onBtnTouchEvent);
					btn.removeEventListener(TouchEvent.TOUCH_END, onBtnTouchEvent);
					btn.removeEventListener(TouchEvent.TOUCH_ROLL_OUT, onBtnTouchEvent);
					btn.removeEventListener(TouchEvent.TOUCH_TAP, onBtnTouchEvent);
				} else {
					btn.buttonMode = false;
					btn.removeEventListener(MouseEvent.MOUSE_DOWN, onBtnMouseEvent);
					btn.removeEventListener(MouseEvent.ROLL_OVER, onBtnMouseEvent);
					btn.removeEventListener(MouseEvent.MOUSE_UP, onBtnMouseEvent);
					btn.removeEventListener(MouseEvent.ROLL_OUT, onBtnMouseEvent);
					btn.removeEventListener(MouseEvent.CLICK, onBtnMouseEvent);
				}
			}
		}
		
		protected function onBtnTouchEvent(e:TouchEvent):void
		{
			
		}
		protected function onBtnMouseEvent(e:MouseEvent):void
		{
			
		}
		
		protected function onBtnMajorInteraction(btn:BaseSpriteSheet):void
		{
			
		}
		
		protected function reset():void
		{
			
		}
		
		// ================================================================================================================================
		// PUBLIC STATIC
		// ================================================================================================================================
		
		/**
		 * XML must be like this:
		 * 
		 * <button id="_btnSinglePlayer" 	pngSheetID="main_menu_sheet_singlePlayerBtnPNG" xmlSheetID="main_menu_sheet_singlePlayerBtnXML" x="330" y="370"/>
		 */		
		public static function createSimpleSpriteSheet(xml:XML, gameEngine:IMenuToEngine):SimpleSpriteSheetCPU
		{
			var btn:SimpleSpriteSheetCPU = 
				new SimpleSpriteSheetCPU((gameEngine.getGlobalAssetContent(xml.@pngSheetID, AssetTypes.SWF) as Bitmap).bitmapData, 
									      gameEngine.getGlobalAssetContent(xml.@xmlSheetID, AssetTypes.XML) as XML);
			btn.x = Number(xml.@x);
			btn.y = Number(xml.@y);
			btn.name = String(xml.@id);
			
			return btn;
		}
		
		/**
		 * XML must be like this:
		 * 
		 * <button id="_btnOptions" x="10" y="640">
		 *		<sheets>
		 *			<sheet pngSheetID="main_menu_sheet_optionsBtn1PNG" 	xmlSheetID="main_menu_sheet_optionsBtn1XML"/>
		 *			<sheet pngSheetID="main_menu_sheet_optionsBtn2PNG" 	xmlSheetID="main_menu_sheet_optionsBtn2XML"/>
		 *		</sheets>
		 *	</button>
		 */		
		public static function createComplexSpriteSheet(xml:XML, gameEngine:IMenuToEngine):ComplexSpriteSheetCPU
		{
			var bmpDataSheets:Vector.<BitmapData> = new Vector.<BitmapData>(),
				settingsXMLs:Vector.<XML> = new Vector.<XML>(),
				sheetsXmlList:XMLList = xml.sheets[0].sheet,
				sheetXml:XML;
			for each (sheetXml in sheetsXmlList) {
				bmpDataSheets.push((gameEngine.getGlobalAssetContent(sheetXml.@pngSheetID, AssetTypes.SWF) as Bitmap).bitmapData);
				settingsXMLs.push(gameEngine.getGlobalAssetContent(sheetXml.@xmlSheetID, AssetTypes.XML) as XML);
			}
			
			var btn:ComplexSpriteSheetCPU = new ComplexSpriteSheetCPU(bmpDataSheets, settingsXMLs);
			
			btn.x = Number(xml.@x);
			btn.y = Number(xml.@y);
			btn.name = String(xml.@id);
			
			return btn;
		}
		
		/**
		 * XML must be like this:
		 * 
		 * <button id="_btnSinglePlayer" 	pngSheetID="main_menu_sheet_singlePlayerBtnPNG" xmlSheetID="main_menu_sheet_singlePlayerBtnXML" x="330" y="370">
		 * 		<states>
		 *			<state id="idle" 		startFrame="1" 	endFrame="1"/>
		 *			<state id="rollOver" 	startFrame="1"  endFrame="20"/>
		 *			<state id="rollOut" 	startFrame="20" endFrame="1"/>
		 *		</states>
		 * </button>
		 */
		public static function createFrameLabeled_SimpleSpriteSheet(xml:XML, gameEngine:IMenuToEngine):FrameLabeledSpriteSheet
		{
			var sp:SimpleSpriteSheetCPU = createSimpleSpriteSheet(xml, gameEngine),
				btn:FrameLabeledSpriteSheet = new FrameLabeledSpriteSheet(xml.@name, sp, xml.states[0]);
			
			btn.x = Number(xml.@x);
			btn.y = Number(xml.@y);
			btn.name = String(xml.@id);
				
			return btn;
		}
		
		/**
		 * XML must be like this:
		 * 
		 * <button id="_btnOptions" x="10" y="640">
		 *		<sheets>
		 *			<sheet pngSheetID="main_menu_sheet_optionsBtn1PNG" 	xmlSheetID="main_menu_sheet_optionsBtn1XML"/>
		 *			<sheet pngSheetID="main_menu_sheet_optionsBtn2PNG" 	xmlSheetID="main_menu_sheet_optionsBtn2XML"/>
		 *		</sheets>
		 *		
		 *		<states>
		 *			<state id="idle" 		startFrame="1" 	endFrame="1"/>
		 *			<state id="rollOver" 	startFrame="1"  endFrame="20"/>
		 *			<state id="rollOut" 	startFrame="20" endFrame="1"/>
		 *			<state id="rotateOpen" 	startFrame="20" endFrame="40"/>
		 *			<state id="rotateClose" startFrame="40" endFrame="20"/>
		 *		</states>
		 * </button>
		 */	
		public static function createFrameLabeled_ComplexSpriteSheet(xml:XML, gameEngine:IMenuToEngine):FrameLabeledSpriteSheet
		{
			var sp:ComplexSpriteSheetCPU = createComplexSpriteSheet(xml, gameEngine),
				btn:FrameLabeledSpriteSheet = new FrameLabeledSpriteSheet(xml.@name, sp, xml.states[0]);
			
			btn.x = Number(xml.@x);
			btn.y = Number(xml.@y);
			btn.name = String(xml.@id);
			
			return btn;
		}

		// ================================================================================================================================
		// GETTERS, SETTERS
		// ================================================================================================================================
		
		public function get TYPE()	:	uint 	{ return _type; }

		public function get ID()	:	String 	{ return _id;	}

		public function get backMenuID()		: String 	{ return _backMenuID; 	}
		public function set backMenuID(v:String): void 		{ _backMenuID = v;		}
	}
}